@echo off
setlocal enabledelayedexpansion

set buildType=%1
set branch=%2
set clean=%3
set logFile="-"
set basePath=C:\mateurp_pipline\mateurp
set projectPath=%basePath%\mateurp
if not "%buildType%"=="DEBUG" (
    if not "%buildType%"=="RELEASE" (
        echo param must be 'DEBUG' or 'RELEASE'
        exit /b 1
    )
)


if "%branch%" == "" (
	echo branch must be set
    exit /b 1
)

cd /d %basePath%
:: Update Code
echo Update Code Start
git checkout %branch%
git fetch origin || exit /b 1
git reset --hard "origin/%branch%" || exit /b 1
echo Update Code Finish

echo Check Changed
set curr_buildId=%basePath%\mateurp\Build\build_id_%buildType%.txt
set last_buildId=%basePath%\mateurp\Build\build_id_%buildType%_last.txt
git log -1 --format=%%h origin/%branch%>%curr_buildId%
:: 检查文件是否存在
if exist "%last_buildId%" (
	if exist "%basePath%\mateurp\Build\Android\unityLibrary.aar" (
	    echo "last file exist!"
		fc /b "%curr_buildId%" "%last_buildId%" > nul
		:: 获取 FC 命令的错误级别 (Error Level)
		if !errorlevel! equ 0 (
			 echo aar exists and not need update!
			 goto eof
		)
		del %projectPath%\Build\Android\unityLibrary.aar
	)
)
echo End Check Changed

:: GenerateAll
if "%clean%" == "true" (
echo GenerateAll Start
"C:\Program Files\Unity\Hub\Editor\2022.3.61f1c1\Editor\Unity.exe" -buildTarget Android -batchmode -nographics -quit -projectPath "%projectPath%" -executeMethod HybridCLR.Editor.BuildAssetsCommand.Prebuild -logFile %logFile% || exit /b 1
echo GenerateAll Finish
)

:: Copy ClothShaderVariants
set artvariants=C:\mateurp_pipline\mate-unity-cloth\Assets\_Art\ClothShaderVariants.shadervariants
copy /Y %artvariants% %projectPath%\Assets\_Art\ClothShaderVariants.shadervariants
copy /Y %artvariants% C:\mateurp_pipline\Mate\ShaderVariants\ClothShaderVariants.shadervariants

:: BuildAssets
echo BuildAssets Start

if "%clean%" == "true" (
	rd /s /q  %projectPath%\Assets\StreamingAssets
	rd /s /q  %projectPath%\HybridCLRBuildCache
	rd /s /q  %projectPath%\Build\Android\%buildType%\MateAi
)

"C:\Program Files\Unity\Hub\Editor\2022.3.61f1c1\Editor\Unity.exe" -batchmode -nographics -quit -projectPath "%projectPath%" -executeMethod HybridCLR.Editor.BuildAssetsCommand.BuildAndCopyABAOTHotUpdateDlls -logFile %logFile% || exit /b 1
echo BuildAssets Finish

:: BuildArts
::echo BuildArts Start
::"C:\Program Files\Unity\Hub\Editor\2022.3.61f1c1\Editor\Unity.exe" -batchmode -nographics -quit -projectPath "%projectPath%" -executeMethod HybridCLR.Editor.BuildAssetsCommand.BuildBuiltInArtAssetsAB -logFile %logFile% || exit /b 1
::echo BuildArts Finish

:: ExportProject
echo ExportProject Start

set exportAsAndroidProject=True
"C:\Program Files\Unity\Hub\Editor\2022.3.61f1c1\Editor\Unity.exe" -batchmode -nographics -quit -projectPath "%projectPath%" -executeMethod BuildTool.BuildAPKNoABScript -logFile %logFile% || exit /b 1
echo ExportProject Finish

:: AndroidStudioBuild
echo AndroidStudioBuild Start
cd /d %projectPath%\Build\Android\%buildType%\MateAi
rd /s /q  .\launcher
call C:\ProgramData\gradle-7.5.1\bin\gradle wrapper
call gradlew clean

if "%buildType%"=="DEBUG" (
    call gradlew assembleDebug || call gradlew assembleDebug || exit 1
	copy /Y %projectPath%\Build\Android\DEBUG\MateAi\unityLibrary\build\outputs\aar\unityLibrary-debug.aar %projectPath%\Build\Android\unityLibrary.aar 
) else (
    call gradlew assembleRelease || call gradlew assembleRelease || exit 1
	copy /Y %projectPath%\Build\Android\RELEASE\MateAi\unityLibrary\build\outputs\aar\unityLibrary-release.aar %projectPath%\Build\Android\unityLibrary.aar 
)
copy /Y %curr_buildId% %last_buildId%
echo AndroidStudioBuild Finish
:eof
endlocal